﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace fhe
{
  namespace math
  {
    public static class ShapeUtils
    {
      public enum Type
      {
        AABB,
        Circle,
        Polygon,
      };

      public static IShape GetBoundingShape(List<Vec2> points, Type type)
      {
        switch (type)
        {
          case Type.AABB:
            return GetBoundingBox(points);
          case Type.Circle:
            return GetBoundingCircle(points);
          case Type.Polygon:
            return GetBoundingPolygon(points);
          default:
            return default(IShape);
        }
      }

      public static Circle GetBoundingCircle(List<Vec2> points)
      {
        Vec2 center = points.Aggregate((v1, v2) => v1 + v2) / (double)points.Count;
        return new Circle()
        {
          Center = center,
          Radius = points.Max(point => (point - center).Length()),
        };
      }

      public static AABB GetBoundingBox(List<Vec2> points)
      {
        return new AABB()
        {
          Min = new Vec2(points.Select(v => v.X).Min(), points.Select(v => v.Y).Min()),
          Max = new Vec2(points.Select(v => v.X).Max(), points.Select(v => v.Y).Max()),
        };
      }

      public static Polygon GetBoundingPolygon(List<Vec2> points)
      {
        return new Polygon() { Vertices = points };
      }
    }

    public interface IShape
    {
      bool Inside(Vec2 v);

      Vec2 SurfaceOffset(Vec2 v);
    }
  }
}
